import aspects

from kidux.apps.im.config import DEBUG

from Controller import Controller as EmeseneController
from ConversationManager import ConversationManager as EmeseneConversationManager
from Conversation import Conversation as EmeseneConversation
from ContactManager import ContactManager as EmeseneContactManager
from abstract.ContactManager import ContactManager as EmeseneAbstractContactManager
from UserPanel import UserPanel as EmeseneUserPanel
from UserList import UserList as EmeseneUserList
from UserMenu import UserMenu as EmeseneUserMenu
from FileTransfer import FileTransfer as EmeseneFileTransfer
from ConversationUI import ToolbarWidget as EmeseneToolbarWidget
from ConversationWindow import ConversationWindowMenu as EmeseneConversationWindowMenu

class _WrapperManager(object):
    def __init__(self):
        self.plugin = None

    def activate(self):
        aspects.with_wrap(self.personalStatusChanged, EmeseneController.on_self_status_changed)
        aspects.with_wrap(self.personalNickChanged, EmeseneUserPanel.selfNickChanged)
        aspects.with_wrap(self.personalMessageChanged, EmeseneUserPanel.personalMessageChanged)
        aspects.with_wrap(self.personalAvatarChanged, EmeseneController.changeAvatar)
        aspects.with_wrap(self.disconnect, EmeseneController.quit)
        aspects.with_wrap(self.logOutgoingMessage, EmeseneConversationManager.do_send_message)
        aspects.with_wrap(self.logIncomingMessage, EmeseneConversationManager.do_receive_message)
        aspects.with_wrap(self.newConversation, EmeseneConversationManager.newConversation)
        aspects.with_wrap(self.openConversation, EmeseneConversationManager.openConversation)
        aspects.with_wrap(self.typingMessage, EmeseneConversation.receiveTyping)
        aspects.with_wrap(self.userJoin, EmeseneConversation.userJoin)
        aspects.with_wrap(self.userLeave, EmeseneConversation.userLeave)
        aspects.with_wrap(self.addContact, EmeseneContactManager.add)
        aspects.with_wrap(self.removeContact, EmeseneContactManager.remove)
        aspects.with_wrap(self.contactMessageChanged, EmeseneAbstractContactManager._on_contact_message_changed)
        aspects.with_wrap(self.contactAvatarChanged, EmeseneUserList.getContactImage)
        aspects.with_wrap(self.fileReceived, EmeseneFileTransfer.onFtReceived)
        aspects.with_wrap(self.fileTransfer, EmeseneConversation.onFtInvite)
        aspects.with_wrap(self.fileSend, EmeseneToolbarWidget.sendFileClicked)
        aspects.with_wrap(self.menuFileSend, EmeseneConversationWindowMenu.onSendFileActivate)
        aspects.with_wrap(self.contactNewGroup, EmeseneContactManager.move_to_group)
        aspects.with_wrap(self.blockContact, EmeseneUserMenu.on_send_mail_activate)
        aspects.with_wrap(self.blockContact, EmeseneUserMenu.on_profile_activate)
        aspects.with_wrap(self.blockInvite, EmeseneConversation.inviteUser)
        aspects.with_wrap(self.blockLogin, EmeseneController.onInitialStatusChange)

    def deactivate(self):
        aspects.without_wrap(self.personalStatusChanged, EmeseneController.on_self_status_changed)
        aspects.without_wrap(self.personalNickChanged, EmeseneUserPanel.selfNickChanged)
        aspects.without_wrap(self.personalMessageChanged, EmeseneUserPanel.personalMessageChanged)
        aspects.without_wrap(self.personalAvatarChanged, EmeseneController.changeAvatar)
        aspects.without_wrap(self.disconnect, EmeseneController.quit)
        aspects.without_wrap(self.logOutgoingMessage, EmeseneConversationManager.do_send_message)
        aspects.without_wrap(self.logIncomingMessage, EmeseneConversationManager.do_receive_message)
        aspects.without_wrap(self.newConversation, EmeseneConversationManager.newConversation)
        aspects.without_wrap(self.openConversation, EmeseneConversationManager.openConversation)
        aspects.without_wrap(self.typingMessage, EmeseneConversation.receiveTyping)
        aspects.without_wrap(self.userJoin, EmeseneConversation.userJoin)
        aspects.without_wrap(self.userLeave, EmeseneConversation.userLeave)
        aspects.without_wrap(self.addContact, EmeseneContactManager.add)
        aspects.without_wrap(self.removeContact, EmeseneContactManager.remove)
        aspects.without_wrap(self.contactMessageChanged, EmeseneContactManager.__on_contact_message_changed)
        aspects.without_wrap(self.contactAvatarChanged, EmeseneUserList.getContactImage)
        aspects.without_wrap(self.fileReceived, EmeseneFileTransfer.onFtReceived)
        aspects.without_wrap(self.fileTransfer, EmeseneConversation.onFtInvite)
        aspects.without_wrap(self.fileSend, EmeseneToolbarWidget.sendFileClicked)
        aspects.without_wrap(self.menuFileSend, EmeseneConversationWindow.onSendFileActivate)
        aspects.without_wrap(self.contactNewGroup, EmeseneContactManager.move_to_group)
        aspects.without_wrap(self.blockContact, EmeseneUserMenu.on_send_mail_activate)
        aspects.without_wrap(self.blockContact, EmeseneUserMenu.on_profile_activate)
        aspects.without_wrap(self.blockInvite, EmeseneConversation.inviteUser)
        aspects.without_wrap(self.blockLogin, EmeseneController.onInitialStatusChange)

    def personalStatusChanged(self, controller, msn, status):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.personalStatusChanged(controller, msn, status)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def personalNickChanged(self, userPanel, msn, oldNick, nick):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.personalNickChanged(userPanel, msn, oldNick, nick)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def personalMessageChanged(self, userPanel, msn, accountId, message):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.personalMessageChanged(userPanel, msn, accountId, message)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def personalAvatarChanged(self, controller, imagePath):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.personalAvatarChanged(controller, imagePath)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def disconnect(self, controller, status):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.disconnect(controller, status)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def logOutgoingMessage(self, conversationManager, conversation, message):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.logOutgoingMessage(conversationManager, conversation, message)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def logIncomingMessage(self, conversationManager, conversation, sender, senderName, message, format, charset):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.logIncomingMessage(conversationManager, conversation, sender, senderName, message, format, charset)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def newConversation(self, conversationManager, msn, accountId, switchboard, weStarted):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.newConversation(conversationManager, msn, accountId, switchboard, weStarted)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def openConversation(self, conversationManager, msn, accountId, weStarted, switchboard):
        return self.newConversation(conversationManager, msn, accountId, switchboard, weStarted)

    def typingMessage(self, conversation, switchboard, contactId):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.typingMessage(conversation, switchboard, contactId)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def userJoin(self, conversation, switchboard, contactId):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.userJoin(conversation, switchboard, contactId)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def userLeave(self, conversation, switchboard, contactId):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.userLeave(conversation, switchboard, contactId)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def addContact(self, contactManager, contactId, group=""):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.addContact(contactManager, contactId, group)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def removeContact(self, contactManager, contactId):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.removeContact(contactManager, contactId)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def contactMessageChanged(self, contactManager, contactId, message):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.contactMessageChanged(contactManager, contactId, message)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def contactAvatarChanged(self, userList, contact):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.contactAvatarChanged(userList, contact)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def fileReceived(self, fileTransfer, p2pUser, session, context, src, sender):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.fileReceived(fileTransfer, p2pUser, session, context, src, sender)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def fileTransfer(self, conversation, p2pUser, session, context, receiver):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.fileTransfer(conversation, p2pUser, session, context, receiver)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def fileSend(self, toolBar, toolButton):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.fileSend(toolBar, toolButton)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def menuFileSend(self, conversationMenu, menuItem):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.menuFileSend(conversationMenu, menuItem)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def contactNewGroup(self, contactManager, contactId, oldName, newName):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.contactNewGroup(contactManager, contactId, oldName, newName)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def blockContact(self, userMenu, menuItem):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.blockContact(userMenu, menuItem)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def blockInvite(self, conversation, contactId):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.blockInvite(conversation, contactId)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop

    def blockLogin(self, controller, msn, status, _, contactInfo):
        proceed = True
        if self.plugin is not None:
            proceed = self.plugin.blockLogin(controller, msn, status, _, contactInfo)

        if proceed:
            yield aspects.proceed
            yield aspects.return_stop


wrappers = _WrapperManager()

